﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;
using SyndicateBusiness.Classes;

namespace BigLotterySyndicate.Login
{
    public partial class Reset : BasePublicPage
    {
        public string UserNameValue
        {
            get { return this.TextBoxUserName.Text; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonLogIn_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                this.TextBoxUserName.Text = "";
                this.PlaceHolderResetSuccess.Visible = false;
                return;
            }
            user user = this.BusinessFacade.UserBL.GetAll(this.BusinessFacade.CurrentSyndicate.id).SingleOrDefault(u => u.email.ToLower() == UserNameValue.ToLower());

            string password = GenerateNewPassword();

            user.password = Utilities.EncodePassword(password);
            user.isActive = false;
            bool result = this.BusinessFacade.UserBL.UpdateUser();

            if (result)
            {
                SetResetPasswordLogEntry(user, password);

                this.TextBoxUserName.Text = "";
                PlaceHolderResetSuccess.Visible = true;
            }
         
        }

        private void SetResetPasswordLogEntry(user u, string password)
        {
            fundsLog log = new fundsLog();

            log.amount = -1;
            log.description = "Password reset token created: " + password;
            log.fromUserId = u.id;
            log.toUserId = u.id;
            log.syndicateId = u.syndicateId;
            log.moneyIn = true;
            log.dateCreated = DateTime.Now;

            this.BusinessFacade.FundsLogBL.InsertEntry(log);
        }

        public static string GenerateNewPassword()
        {
            System.Guid guid = System.Guid.NewGuid();

            String[] strGuid = guid.ToString().Split('-');

            return "R" + strGuid[0];
        }


        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            bool validAccount = this.BusinessFacade.UserBL.ValidateAccount(UserNameValue, false,this.BusinessFacade.CurrentSyndicate.id);

            if (validAccount)
            {
                user u = this.BusinessFacade.UserBL.GetAll(this.BusinessFacade.CurrentSyndicate.id).SingleOrDefault(r => r.email == UserNameValue);

                fundsLog logentry = null;
                if (u != null)
                    logentry =
                        this.BusinessFacade.FundsLogBL.SelectAllByToUserId(u.id).OrderByDescending(l => l.dateCreated).
                            FirstOrDefault(l => l.amount == -1);

                if (logentry != null)
                {
                    CustomValidator1.ErrorMessage = "You've already requested your password to be reset. Please contact your syndicate manager for your temporary password.";
                    args.IsValid = false;
                }
                else
                    args.IsValid = true;
            }
            else
            {
                CustomValidator1.ErrorMessage = "Entered credentials are not valid. Access has been denied";
                args.IsValid = false;
            }

           
        }
    }
}